set(MODULE dbms)
execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/dbms
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

message("PROTOC_CMD: ${PROTOC_DIR}/protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/dbms --proto_path=${CMAKE_CURRENT_SOURCE_DIR}")
execute_process(
    COMMAND  ${PROTOC_DIR}/protoc --cpp_out=lite:${CMAKE_CURRENT_BINARY_DIR}/dbms --proto_path=${CMAKE_CURRENT_SOURCE_DIR} dbms.proto rpc.proto
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    RESULT_VARIABLE command_result
    OUTPUT_VARIABLE command_output
    ERROR_VARIABLE command_error
)

if(NOT command_result EQUAL 0)
    message(FATAL_ERROR "Command protoc failed with exit code ${command_result}:\n${command_error}")
endif()

set(absl_DIR ${PRODUCT_INSTALL_DIR}/protobuf/lib/cmake/absl)

# set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PRODUCT_INSTALL_DIR}/lib/cmake/absl;${PRODUCT_INSTALL_DIR}/lib/cmake/protobuf/")
# find_package(protobuf CONFIG)
set(utf8_range_DIR ${PRODUCT_INSTALL_DIR}/protobuf/lib/cmake/utf8_range)
find_package(absl)
find_package(utf8_range)

message(INFO "absl: ${absl_FOUND}, ${absl_LIBRARIES}")

aux_source_directory(./ SRC)
aux_source_directory(${CMAKE_CURRENT_BINARY_DIR}/dbms SRC)

add_library(${MODULE} ${SRC})

target_link_directories(${MODULE} PUBLIC ${SQLITECPP_LIBS_DIR} ${PROTOBUF_LIBS_DIR})
target_link_libraries(${MODULE} SQLiteCpp sqlite3 protobuf-lite absl::string_view
    absl::base absl::log absl::hash absl::strings absl::cordz_info absl::container_common
    absl::cord absl::btree absl::flat_hash_map absl::log_internal_check_op
    utf8_range::utf8_range utf8_range::utf8_validity
    )
target_include_directories(${MODULE} PUBLIC ./
    ${SQLITECPP_INCLUDE_DIR}
    ${PROTOBUF_INCLUDE_DIR}
    ${ABSL_INCLUDE_DIRS}
    ${CMAKE_CURRENT_BINARY_DIR}/dbms)